package com.rzjm.api.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.commons.base.SearchParam;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.locks.Condition;

public class MapperUtil {
    /**
     * 是否加载 查询条件
     *
     * @param cnd
     * @param k
     * @param v
     * @return
     */
    public static boolean idLoadCnd(String cnd, String k, Object v) {
        return k.startsWith(cnd) && null != v && v.toString().length() > 0;
    }

    /**
     * 加载 搜索条件
     *
     * @param searchParams
     * @param qw
     */
    public static void loadSearchParam(Map<String, Object> searchParams, QueryWrapper qw) {
        if (null != searchParams) {
            searchParams.forEach((k, v) -> {
                if (idLoadCnd(SearchParam.SEARCH_EQ, k, v)) {
                    qw.eq(underscoreName(k.split(SearchParam.SEARCH_EQ)[1]), v);
                } else if (idLoadCnd(SearchParam.SEARCH_LLIKE, k, v)) {
                    qw.likeLeft(underscoreName(k.split(SearchParam.SEARCH_LLIKE)[1]),String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_RLIKE, k, v)) {
                    qw.likeRight(underscoreName(k.split(SearchParam.SEARCH_RLIKE)[1]), String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_LIKE, k, v)) {
                    qw.like(underscoreName(k.split(SearchParam.SEARCH_LIKE)[1]), String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_GE, k, v)) {//大于等于
                    qw.ge(underscoreName(k.split(SearchParam.SEARCH_GE)[1]), String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_GT, k, v)) {//大于
                    qw.gt(underscoreName(k.split(SearchParam.SEARCH_GT)[1]), String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_LE, k, v)) {//小于等于
                    String ss = underscoreName(k.split(SearchParam.SEARCH_LE)[1]);
                    System.out.println("ss:" + ss);
                    String vv = String.valueOf(v);
                    System.out.println("vv:" + vv);
                    qw.le(ss, String.valueOf(v));
                } else if (idLoadCnd(SearchParam.SEARCH_LT, k, v)) {//小于
                    qw.lt(underscoreName(k.split(SearchParam.SEARCH_LT)[1]), String.valueOf(v));
                }

            });
        }
    }

    /**
     * 加载 排序条件
     */
    public static void loadSort(Map<String, String> sorts, QueryWrapper qw) {
        if (null != sorts && sorts.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            sorts.forEach((k, v) -> {
                if("asc".equals(v.toLowerCase())){
                    qw.orderByAsc(k);
                }else{
                    qw.orderByDesc(k);
                }
            });
        }
    }

    public static String underscoreName(String name) {
        if (StrUtil.isEmpty(name)) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        result.append(lowerCaseName(name.substring(0, 1)));
        for (int i = 1; i < name.length(); i++) {
            String s = name.substring(i, i + 1);
            String slc = lowerCaseName(s);
            if (!s.equals(slc)) {
                result.append("_").append(slc);
            } else {
                result.append(s);
            }
        }
        return result.toString();
    }

    public static String lowerCaseName(String name) {
        return name.toLowerCase(Locale.US);
    }


    public static void loadWrapper(QueryWrapper wrapper, Condition cnd){
        if(wrapper!=null){
        }
    }
}
